package leetcode.day_2020_11_9_11_15;

public class L_19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) {
            return null;
        }
        ListNode root = new ListNode(-1);
        root.next = head;
        ListNode fast = root;
        ListNode slow = root;
        while (fast != null && --n>=0) {
            fast = fast.next;
        }
        while (fast.next!=null) {
            slow = slow.next;
            fast = fast.next;
        }
        // 删除节点操作
        slow.next = slow.next.next;
        return root.next;
    }
}
